﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Oracle.DataAccess.Client;
using System.Windows.Forms;
using System.Data;

namespace DB2CSV
{
    class Polaczenie
    {
        public OracleConnection conn;
        string zapytanie;
        DataSet set;
        DataTable table;
        OracleDataAdapter adapter;

        public Polaczenie(DaneUzytkownika nowy)
        {
            string ConnString = String.Format("Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={0})(PORT={1})))"
                +"(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME={2})));User Id={3};Password={4}",nowy.Host,nowy.Port,nowy.Database,nowy.UserID,nowy.Password);
            try
            {
                conn = new OracleConnection(ConnString);
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }
        public void Polacz()
        {
            try
            {
                conn.Open();
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }
        public void Rozlacz()
        {
            if (conn.State == ConnectionState.Open)
            {
                try
                {
                    conn.Close();
                }
                catch (Exception exp)
                {
                    MessageBox.Show(exp.Message);
                }
            }
        }
        public ConnectionState Status
        {
            get { return conn.State; }
        }
        public DataTable PokazKolumny(string NazwaTabeli)
        {
            zapytanie = String.Format("SELECT column_name kolumna, data_type typ FROM user_tab_cols WHERE lower(table_name) = \'{0}\'",NazwaTabeli.ToLower());
            set = new DataSet();
            adapter = new OracleDataAdapter(zapytanie, conn);
            adapter.Fill(set);
            table = set.Tables[0];
            return table;
        }

    }
}
